<?php
namespace App\Services;

use App\Models\User;
use App\Models\UserSafe;
use Illuminate\Support\Facades\DB;

/**
 *
 * 用户服务层
 *
 * @author maqu
 *
 */
class UserService extends BaseService {
//
//    public function loginBySMS($phone,$code)
//    {
//        //$code
//        //验证码验证请在controller层处理
//
//        $time =gmtime();
//
//        $user = User::where('user_name',$phone)->where('user_state',0)->first();
//        if(!$user){
//            //create if not exist
//            $user = new User();
//
//            $user->user_name=$phone;
//            $user->nickname=substr_replace($phone,'****',3,4);
//            $user->password =md5(uuid());
//            $user->phone_mob=$phone;
//            $user->reg_time =$time;
//
//            $user->save();
//        }
//
//        //get user info
//
//        // Retrieve current user info
//        $info = array();
//
//        // Data fields to return
//        // birthday just below refer to baby's birthday
//        $fields = array('user_id', 'user_name', 'phone_mob','portrait','nickname','wx_nickname');
//
//        // Loop through fields and set key/value of the info array
//        foreach ($fields as $field) {
//            $info[$field] = $user->$field;
//        }
//
//        $info['portrait']=complete_url($info['portrait'].'?t='.gmtime());
//
//        $access_token = uuid();
//        $info['access_token'] =$access_token;
//        $logintime = $time;
//
//        //insert into user_safe
//        $userSafe = UserSafe::find($user->user_id);
//        if($userSafe){
//
//            $userSafe->login_time=$time;
//            $userSafe->login_pwd= $user->password;
//            $userSafe->expire_time=$time+7*24*60*60;
//            $userSafe->access_token=$access_token;
//            $userSafe->save();
//        } else {
//
//            $userSafe = new UserSafe();
//
//            $userSafe->user_id=$user->user_id;
//            $userSafe->login_time=$time;
//            $userSafe->login_pwd= $user->password;
//            $userSafe->expire_time=$time+7*24*60*60;
//            $userSafe->access_token=$access_token;
//            $userSafe->save();
//        }
//
//        event('user.login',$user);
//
//        return $this->success($info);
//
//    }

    //账户绑定
    public function bind_mobile($user_id,$phone)
    {
        //$code
        //验证码验证请在controller层处理

        $time =gmtime();

        //老账户
        $old_account = User::find($user_id);
        $old_userSafe = UserSafe::find($user_id);
        if(!$old_account || !$old_userSafe){
            return $this->failure('该用户不存在!');
        }

        if($old_account->phone_mob){
            return $this->failure('该账户已绑定手机号');
        }

        //不存在的场合,创建账户
        $user = User::where('user_name',$phone)->where('user_state',0)->first();
        if(!$user){
            //create if not exist
            $user = new User();

            $user->user_name=$phone;
            $user->nickname=substr_replace($phone,'****',3,4);
            $user->password =$old_userSafe->login_pwd;
            $user->phone_mob=$phone;
            $user->reg_time =$time;

            $user->wx_nickname=$old_account->wx_nickname;
            $user->wx_openid=$old_account->wx_openid;
            $user->wx_unionid=$old_account->wx_unionid;
            $user->wx_openid_pms=$old_account->wx_openid_pms;
            $user->wb_nickname =$old_account->wb_nickname;
            $user->wb_openid=$old_account->wb_openid;
            $user->qq_nickname =$old_account->qq_nickname;
            $user->qq_openid =$old_account->qq_openid;
            $user->portrait =$old_account->portrait;
            $user->nickname =$old_account->nickname;
            $user->gender=$old_account->gender;
            $user->save();

            DB::table('user_safe')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);

        } else {
            //已存在的场合，设置老账户的第三方登录信息

            if($old_account->wx_nickname){
                $user->wx_nickname=$old_account->wx_nickname;
            }
            if($old_account->wx_openid){
                $user->wx_openid=$old_account->wx_openid;
            }
            if($old_account->wx_unionid){
                $user->wx_unionid=$old_account->wx_unionid;
            }
            if($old_account->wx_openid_pms){
                $user->wx_openid_pms=$old_account->wx_openid_pms;
            }

            if($old_account->wb_nickname){
                $user->wb_nickname =$old_account->wb_nickname;
            }
            if($old_account->wb_openid){
                $user->wb_openid=$old_account->wb_openid;
            }
            if($old_account->qq_nickname){
                $user->qq_nickname =$old_account->qq_nickname;
            }
            if($old_account->qq_openid){
                $user->qq_openid =$old_account->qq_openid;
            }

            if(!$user->portrait){
                $user->portrait =$old_account->portrait;
            }

            if(!$user->nickname){
                $user->nickname =$old_account->nickname;
            }
            $user->gender=$old_account->gender;
            $user->password =$old_userSafe->login_pwd;

            $user->save();

            //insert into user_safe
            //因为绑定后需要冲掉用手机登陆的情报，所以用老账号替换新账号
            $userSafe = UserSafe::find($user->user_id);
            if($userSafe){

                $userSafe->login_time=$old_userSafe->login_time;
                $userSafe->login_pwd= $old_userSafe->login_pwd;
                $userSafe->expire_time=$old_userSafe->expire_time;
                $userSafe->access_token=$old_userSafe->access_token;
                $userSafe->save();
            } else {

                $userSafe = new UserSafe();

                $userSafe->user_id=$user->user_id;
                $userSafe->login_time=$old_userSafe->login_time;
                $userSafe->login_pwd= $old_userSafe->login_pwd;
                $userSafe->expire_time=$old_userSafe->expire_time;
                $userSafe->access_token=$old_userSafe->access_token;
                $userSafe->save();
            }
        }

        // 同步优惠券 tb_user_coupon
        DB::table('user_coupon')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);
        // 同步收藏 tb_collect
        DB::table('collect')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);
        // 同步地址 tb_address
        DB::table('address')->where('user_id',$old_account->user_id)->update(['is_default'=>0]);
        DB::table('address')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);
        // 同步购物车 tb_cart
        DB::table('cart')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);
        // tb_user_hongbao
        DB::table('user_hongbao')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);
        //tb_user_priv
        DB::table('user_priv')->where('user_id',$old_account->user_id)->update(['user_id'=>$user->user_id]);

        // 同步订单（购买前要求绑定,理论上应该）
        //tb_feedback(未使用)
        //tb_goods_qa(未使用)
        //tb_groupbuy_log(未使用)
        //tb_pintuan_cj_tuan_data（购买前要求绑定,理论上应该）
        //tb_pintuan_ms_tuan_data（购买前要求绑定,理论上应该）
        //tb_pintuan_tuan_data（购买前要求绑定,理论上应该）

        //删除老帐户
        $old_account->user_state=-1;
        $old_account->save();

        //get user info

        // Retrieve current user info
        $info = array();

        // Data fields to return
        // birthday just below refer to baby's birthday
        $fields = array('user_id', 'user_name', 'phone_mob','portrait','nickname');

        // Loop through fields and set key/value of the info array
        foreach ($fields as $field) {
            $info[$field] = $user->$field;
        }

        $info['portrait']=complete_url($info['portrait'].'?t='.gmtime());

        $info['access_token'] =$old_userSafe->access_token;

        return $this->success($info);

    }
}